﻿using MediatR;
using Newtonsoft.Json;
using Share;
using System.CJL.API.Application.Command.RawMaterialCommand;
using System.CJL.Domain;
using System.CJL.Infrstructre.Interface;
using System.ComponentModel.DataAnnotations;
using static Share.ParseAPI;

namespace System.CJL.API.Application.CommandHanlder.RawMaterialCommandHanler
{
    public class UpdateInventoryCommandHandler : IRequestHandler<UpdateInventoryCommand, int>
    {
        IRawMaterialRepository _repository;

        IStorageRepository _Srepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        public UpdateInventoryCommandHandler(IRawMaterialRepository repository, IStorageRepository Srepository)
        {
            _repository = repository;
            _Srepository = Srepository;
        }

        public async Task<int> Handle(UpdateInventoryCommand request, CancellationToken cancellationToken)
        {
            var stor = await _Srepository.GetMondel(request.StorageId);
            List<Procuredrequest> Procuredrequestto = await Procuredrequestlist();
            var proto = from pro in Procuredrequestto where pro.IsDelete == false && pro.ProcuredNumber== stor.DetailsNumbering select pro;
            Procuredrequest procured = new Procuredrequest();
            foreach (var item in proto)
            {
                procured = item;
            }
            var rawm = from raw in await _repository.GetAll() where raw.IsDelete == false && raw.RawMaterialNumbering ==procured.FeedstockNumber select raw;
            RawMaterial RawMater = new RawMaterial();
            foreach (var item in rawm)
            {
                RawMater = item;
            }

            RawMater.RawMaterialinventory += stor.DetailsNum;
            return await _repository.UpdateAsync(RawMater);

        }

        public async Task<List<Procuredrequest>> Procuredrequestlist()
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyMCIsInNpZCI6IjE4NTk4MDY0NjEwNjkyMzQxNzYiLCJqdGkiOiI1MDhlMTQyZC1lMzQyLTQxYWMtODA0MC1kMzRhMGQ2ZTU0NTgiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi6ams5rW35rOiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.q2A1wd8sO7CnHgzomExiWHqWY_S_lezUIblFJ-eQKVI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string Procuredrequestl = "http://47.96.140.177:8000/LYH/api/Procured/StorageList";
            string Parse = await cilent.GetAsync(Procuredrequestl);
            var json = JsonConvert.DeserializeObject<ApiResultList<Procuredrequest>>(Parse);
            return json.data.plist;
        }

    }
    /// <summary>
    /// 采购申请表
    /// </summary>
	public class Procuredrequest : Audit
    {
        /// <summary>
        /// 采购Id
        /// </summary>
        [Key]
        public int ProcuredId { get; set; }
        /// <summary>
        /// 采购编号
        /// </summary>
        public string? ProcuredNumber { get; set; }
        /// <summary>
        /// 采购原料编号
        /// </summary>
        public string? FeedstockNumber { get; set; }
        /// <summary>
        /// 采购原料名称
        /// </summary>
        public string? FeedstockName { get; set; }
        /// <summary>
        /// 采购原料数量
        /// </summary>
        public string? FeedstockNum { get; set; }
        /// <summary>
        /// 采购原料型号
        /// </summary>
        public string? Feedstocktype { get; set; }
        /// <summary>
        /// 采购原料尺寸
        /// </summary>
        public string? FeedstockSize { get; set; }
        /// <summary>
        /// 采购预算
        /// </summary>
        public string? ProcuredBudget { get; set; }
        /// <summary>
        /// 采购批次号
        /// </summary>
        public string? LotNumber { get; set; }
        /// <summary>
        /// 采购状态
        /// </summary>
        public string? ProcuredState { get; set; }
        /// <summary>
        /// 采购单价
        /// </summary>
        public string? ProcessPrice { get; set; }
        /// <summary>
        /// 实际采购价格
        /// </summary>
        public string? ProcessActual { get; set; }
        /// <summary>
        /// 付款方式
        /// </summary>
        public string? PayMethods { get; set; }
        /// <summary>
        /// 供应商Id
        /// </summary>
		public int supplierID { get; set; }
        /// <summary>
        /// 逻辑删除 
        /// </summary>
        public bool IsDelete { get; set; }
    }
}
